<?php

namespace CHOMP\SGIBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CuentaAnaliticaType extends AbstractType
{
     
	private $id;
	private $esAdmin;
	
	public function __construct($id, $esAdmin){
    	$this->id = $id;
		$this->esAdmin = $esAdmin;
	}    
	 
	 /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nombre')
			->add('descripcion');
			
			if($this->esAdmin == 'SI'){
				$builder->add('empresa');
			}else{
				$builder->add('empresa', 'entity', array(
                     'class'=>'CHOMPSGIBundle:Empresa',
                     'property'=>'nombre',
                     'query_builder' => function (\CHOMP\SGIBundle\Entity\EmpresaRepository $repository)
                     {
                         return $repository->createQueryBuilder('u')
                                ->where('u.id = ?1 AND u.borrado = 0')
                                ->setParameter(1, $this->id);
                     }
                    )
              )
;			
			} 

			$builder->add('save', 'submit', array(
                     'label'=>'Guardar',
					 'attr'=> array('class'=>'btn middle-first')
				  ));
		;
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'CHOMP\SGIBundle\Entity\CuentaAnalitica'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'chomp_sgibundle_cuentaanalitica';
    }
}
